//
//  SISSGuaranteeCompanyChooseViewController.m
//  SISSAPP
//
//  Created by 袁应荣 on 2018/10/31.
//  Copyright © 2018年 优房网. All rights reserved.
//

#import "SISSGuaranteeCompanyChooseViewController.h"
#import "SISSChooseView.h"
#import "SISSShowView.h"
#import "SISSGuaranteeCompanySearchViewController.h"
#import "SISSGuaranteeCompanyShowViewController.h"
#import "SISSNetworkingManager+HouseProgress.h"

@interface SISSGuaranteeCompanyChooseViewController ()<SISSChooseViewDelegate>

@property (weak, nonatomic) UILabel *titleLabel;
@property (weak, nonatomic) SISSChooseView *commpanyChooseView;
@property (weak, nonatomic) SISSShowView *commpanyAddressView;
@property (weak, nonatomic) UIButton *confirmButton;

@end

@implementation SISSGuaranteeCompanyChooseViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"担保公司";
}

#pragma mark - SISSChooseViewDelegate
- (void)clickChooseActionWithChooseView:(SISSChooseView *)chooseView {
    SISSGuaranteeCompanySearchViewController *vc = [[SISSGuaranteeCompanySearchViewController alloc] init];
    [self.navigationController pushViewController:vc animated:YES];
}

- (void)confirmButtonClickAction {
    
    if (!(self.commpanyChooseView.rightStr.length > 0)) {
        [SISSTools showSVWithTitle:@"请选择担保公司"];
        return;
    }
    if (!(self.commpanyAddressView.rightStr.length > 0)) {
        [SISSTools showSVWithTitle:@"请输入担保公司地址"];
        return;
    }
    
    NSDictionary *parameters = @{
                                 @"spId": [SISSUserAccount shared].spId,
                                 @"soId": [SISSUserAccount shared].soId,
                                 @"ahbpId": self.ahbpId,
                                 @"organName": self.commpanyChooseView.rightStr,
                                 @"organAddress": self.commpanyAddressView.rightStr
                                 };
    [kNetworkManager insertGuaranteeOrganInfoWithParamters:parameters success:^(id responseObject) {
        if ([responseObject[@"code"] integerValue] == 200) {
            SISSGuaranteeCompanyShowViewController *vc = [[SISSGuaranteeCompanyShowViewController alloc] init];
            [self.navigationController pushViewController:vc animated:YES];
        }
    } failed:^(NSError *error) {
        NSLog(@"%@",error);
    }];
}

#pragma mark - UI
- (void)setupSubviews {
    [super setupSubviews];
    
    self.view.backgroundColor = [UIColor groupTableViewBackgroundColor];
    
    UILabel *titleLabel = [UILabel yyr_labelWithText:@"担保公司信息" fontSize:15 textColor:MAIN_TEXT_COLOR];
    [self.view addSubview:titleLabel];
    self.titleLabel = titleLabel;
    
    SISSChooseView *commpanyChooseView = [[SISSChooseView alloc] init];
    commpanyChooseView.leftStr = @"*担保公司名称：";
    commpanyChooseView.rightStr = @"名称";
    commpanyChooseView.delegate = self;
    commpanyChooseView.isShowBottomLine = YES;
    [self.view addSubview:commpanyChooseView];
    self.commpanyChooseView = commpanyChooseView;
    
    SISSShowView *commpanyAddressView = [[SISSShowView alloc] init];
    commpanyAddressView.leftStr = @"*担保公司地址：";
    commpanyAddressView.rightStr = @"{地址}";
    commpanyAddressView.isShowBottomLine = YES;
    [self.view addSubview:commpanyAddressView];
    self.commpanyAddressView = commpanyAddressView;
    
    UIButton *confirmButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [confirmButton setTitle:@"确定" forState:UIControlStateNormal];
    [confirmButton setBackgroundColor:MAIN_COLOR];
    [confirmButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [confirmButton addTarget:self action:@selector(confirmButtonClickAction) forControlEvents:UIControlEventTouchUpInside];
    confirmButton.layer.cornerRadius = 4;
    confirmButton.layer.masksToBounds = YES;
    [self.view addSubview:confirmButton];
    self.confirmButton = confirmButton;
}

#pragma mark - 约束
- (void)initConstraint {
    [super initConstraint];
    
    WeakSelf
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.mas_topLayoutGuideBottom).offset(2 * globelMargin);
        make.left.equalTo(weakSelf.view).offset(2 * globelMargin);
    }];
    
    [self.commpanyChooseView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.titleLabel.mas_bottom).offset(globelMargin);
        make.left.right.equalTo(weakSelf.view);
        make.height.mas_equalTo(5.5 * globelMargin);
    }];
    
    [self.commpanyAddressView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.commpanyChooseView.mas_bottom);
        make.left.right.equalTo(weakSelf.view);
        make.height.mas_equalTo(5.5 * globelMargin);
    }];
    
    [self.confirmButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(weakSelf.view).offset(2 * globelMargin);
        make.top.equalTo(weakSelf.commpanyAddressView.mas_bottom).offset(6 * globelMargin);
        make.right.equalTo(weakSelf.view).offset(-2 * globelMargin);
        make.height.mas_equalTo(5.5 * globelMargin);
    }];
}

@end
